from existence import Existence
from interaction30 import Interaction30
from environmentinput import setEnvironment30, getResultFromEnv30

# Lesson 3 derived Existence class
class Existence30(Existence):
    def __init__(self, steps):
        super().__init__(steps)

    def createTestBench(self):
        setEnvironment30()
        Interaction30.clearInteractionDictionary()
        Interaction30.createPrimitiveInteraction('e1', 'r1', -1)
        Interaction30.createPrimitiveInteraction('e1', 'r2', 1)
        Interaction30.createPrimitiveInteraction('e2', 'r1', -1)
        Interaction30.createPrimitiveInteraction('e2', 'r2', 1)
        Interaction30.createPrimitiveInteraction('e3', 'r1', -1)
        Interaction30.createPrimitiveInteraction('e3', 'r2', 1)
        Interaction30.createPrimitiveInteraction('e4', 'r1', -1)
        Interaction30.createPrimitiveInteraction('e4', 'r2', 1)
        Interaction30.createPrimitiveInteraction('e5', 'r1', -1)
        Interaction30.createPrimitiveInteraction('e5', 'r2', 1)
        Interaction30.createPrimitiveInteraction('e6', 'r1', -1)
        Interaction30.createPrimitiveInteraction('e6', 'r2', 1)

    def start(self, randomFlag = False):
        self.createTestBench()

        cycle = 0
        enactedInteraction = None
        previousExperiment = ''
        experiment = ''
        result = ''

        while cycle < self.steps:
            print('Cycle:', cycle)

            # Set Current Context Interaction
            contextInteraction = enactedInteraction
            print('Context', contextInteraction, sep=' ')

            # Make list of anticipated Interactions
            anticipations = Interaction30.anticipate(enactedInteraction)

            # Choose the Experiment to be performed
            experiment = Interaction30.selectExperiment(anticipations, randomFlag)
            #experiment = Interaction30.selectExperiment(anticipations, randomFlag, experiment)

            # Get Result from Environment
            result = getResultFromEnv30(experiment, previousExperiment, result, randomFlag)

            # Set the Mood according
            previousExperiment = experiment
            enactedInteraction = Interaction30.getEnactedInteraction(experiment, result)
            if enactedInteraction.valence >= 0:
                mood = 'PLEASED'
            else:
                mood = 'PAINED'
            print('Enacted', enactedInteraction, mood, sep=' ')

            # Store the new Learning
            Interaction30.learnCompositeInteraction(contextInteraction, enactedInteraction)

            print('*'*140,'\n')
            cycle += 1